/**
 * 
 */
package loansManagement;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;

import main.Constraints;
import mediasManagement.Book;
import mediasManagement.Dvd;
import mediasManagement.Media;

import subscribersManagement.Subscriber;

import exceptions.BadParametersException;
import exceptions.ChildLoanException;

/**
 * @author virgil
 *
 */
public class Check {
	
	public static int seniority(Calendar bornOrSuscribingDate){
		
		Calendar d1= new GregorianCalendar();
		Calendar d2= (Calendar) bornOrSuscribingDate.clone();
			
		int year=d1.get(Calendar.YEAR)-bornOrSuscribingDate.get(Calendar.YEAR);
		d2.set(Calendar.YEAR, d1.get(Calendar.YEAR));
		if(bornOrSuscribingDate.compareTo(d1)<=0){
			return year;
			
		}
		else{
			return Math.max(0, year-1);}
	}
		
	

	public static boolean isAchild (Subscriber lender) throws BadParametersException{
		if (lender==null)
			throw new BadParametersException();
		return seniority(lender.getBornDate())<=Constraints.AGE_OF_CHILD;
		
	}
	
	public static boolean isOneYearSubscriber (Subscriber lender) throws BadParametersException{
		if (lender==null)
			throw new BadParametersException();
		return seniority(lender.getSubscribingDate())<=1;	
	}
	

	public static boolean isTwoYearSubscriber (Subscriber lender) throws BadParametersException{
		if (lender==null)
			throw new BadParametersException();
		return seniority(lender.getSubscribingDate())==2;	
	}

	public static boolean isSupThreeYearSubscriber (Subscriber lender) throws BadParametersException{
		if (lender==null)
			throw new BadParametersException();
		return seniority(lender.getSubscribingDate())>=3;	
	}
	
	public static boolean canlendDvd (Subscriber lender) throws BadParametersException, ChildLoanException{
		if (isAchild(lender))
			throw new ChildLoanException();
			//return false;
		
		if(isOneYearSubscriber(lender)){
			return lender.getNumberOfDvdLoans() < Constraints.maxLOANS_OneYear_DVD;
			}	
		
		if(isTwoYearSubscriber(lender)){
			return lender.getNumberOfDvdLoans() < Constraints.maxLOANS_TwoYear_DVD;
			}
		
		if(isSupThreeYearSubscriber(lender)){
			return lender.getNumberOfDvdLoans() < Constraints.maxLOANS_supThreeYear_DVD;
			}
		
		return false;
	}
	

	public static boolean canlendBook (Subscriber lender) throws BadParametersException{
		
		if(isOneYearSubscriber(lender)){
			return lender.getNumberOfBookLoans()< Constraints.maxLOANS_OneYear_Book;
			}	
		
		if(isTwoYearSubscriber(lender)){
			return lender.getNumberOfBookLoans() < Constraints.maxLOANS_TwoYear_Book;
			}
		
		if(isSupThreeYearSubscriber(lender)){
			return lender.getNumberOfBookLoans() < Constraints.maxLOANS_supThreeYear_Book;
			}
		return false;
	}
	//check a suscriber can lend a media
	public static boolean canLend(Subscriber lender, Media media) throws BadParametersException, ChildLoanException{
		
		if (lender==null)
			throw new BadParametersException();
		
		
		if(media instanceof Dvd)
			return canlendDvd(lender);
		
		if(media instanceof Book)
			return canlendBook(lender);
		
		return false;
	
	}

	//check a suscriber can reserve a media	
		/**
		 * @throws BadParametersException 
		 * @throws ChildLoanException 
		 */
		public static boolean canReserve(Subscriber subscriber, Media media) throws BadParametersException, ChildLoanException{
			
			if (subscriber==null)
				throw new BadParametersException();
			
			if(media instanceof Dvd)
				if (isAchild(subscriber))
					throw new ChildLoanException();
			
			
			return subscriber.getReservations().size()< Constraints.BookDELAY;
		}
}
